/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: Dec 31, 2009
 * Time: 11:17:48 AM
 * To change this template use File | Settings | File Templates.
 */
import dk.brics.automaton.Automaton;
import java.util.*;
import org.jdom.Attribute;
import org.jdom.Element;

// Referenced classes of package dk.brics.dsd:
//            Boolexp, Schema, Regexp, Context,
//            Validator, AttributeDeclaration

class AttributeBoolexp extends Boolexp
{

    AttributeBoolexp(Element element, Schema schema)
    {
        name = element.getAttributeValue("name");
        if(name != null)
        {
            name_local = Schema.pnameLocal(name);
            name_uri = Schema.panameURI(name, element);
        }
        regexp = Regexp.parseOneOptional(element, schema);
    }

    byte evaluate(Context context)
    {
        return evaluate(context, name, name_local, name_uri, regexp, false, false);
    }

    static byte evaluate(Context context, String s, String s1, String s2, Regexp regexp1, boolean flag, boolean flag1)
    {
        Iterator iterator = context.e.getAttributes().iterator();
label0:
        do
        {
            Attribute attribute;
            String s3;
            String s4;
            do
            {
                if(!iterator.hasNext())
                    break label0;
                attribute = (Attribute)iterator.next();
                if(attribute.getNamespaceURI().equals("http://www.brics.dk/DSD/2.0/meta"))
                    continue;
                s3 = null;
                s4 = null;
                if(context.sg != null)
                {
                    s3 = (String)context.v.attribute_whitespace.get(attribute);
                    s4 = (String)context.v.attribute_case.get(attribute);
                }
                if(!Template.isGap(attribute) || context.sg == null)
                    break;
                String s5 = attribute.getName();
                if(s == null || Schema.matchPAname(s1, s2, s5, null))
                {
                    String s7 = Template.getGap(attribute);
                    String s10 = context.v.getAttributeGapDefault(context.e, s5);
                    boolean flag2;
                    boolean flag3;
                    if(s10 != null)
                    {
                        flag2 = false;
                        flag3 = true;
                    } else
                    {
                        flag2 = context.sg.getOpenAttributeGaps().contains(s7);
                        flag3 = context.sg.getClosedAttributeGaps().contains(s7);
                    }
                    if(flag2 && !flag3)
                        return 2;
                    StringEdge stringedge = context.n.lookupStringEdge(s7);
                    if(stringedge == null)
                        return 2;
                    Automaton automaton = stringedge.getLabels();
                    if(s10 != null)
                        automaton = automaton.union(Automaton.makeString(s10));
                    boolean flag4;
                    boolean flag5;
                    if(regexp1 == null)
                    {
                        flag4 = true;
                        flag5 = automaton.isEmpty();
                    } else
                    {
                        automaton = automaton.intersection(Regexp.getAllchars().repeat());
                        automaton = Regexp.normalizeCase(automaton, s4);
                        Automaton automaton1 = regexp1.getAutomaton(context);
                        automaton1 = Regexp.normalizeWhitespace(automaton1, s3);
                        flag4 = automaton.subsetOf(automaton1);
                        flag5 = automaton.intersection(automaton1).isEmpty();
                    }
                    if(flag4 && !flag2)
                        return 1;
                    return (byte) (!flag5 ? 3 : 2);
                }
            } while(true);
            String s6 = attribute.getName();
            String s8;
            if(attribute.getNamespacePrefix().equals(""))
                s8 = null;
            else
                s8 = attribute.getNamespaceURI();
            if(s == null || Schema.matchPAname(s1, s2, s6, s8))
            {
                if(regexp1 == null)
                    return 1;
                String s9 = attribute.getValue();
                if(s3 != null)
                    if(s3.equals("compress"))
                        s9 = Validator.whitespaceString(false, s9);
                    else
                    if(s3.equals("trim"))
                        s9 = Validator.whitespaceString(true, s9);
                if(s4 != null)
                    if(s4.equals("upper"))
                        s9 = s9.toUpperCase();
                    else
                    if(s4.equals("lower"))
                        s9 = s9.toLowerCase();
                if((flag || flag1) && !AttributeDeclaration.checkQname(s9, context.e))
                    return 2;
                return (byte) (!regexp1.match(s9, context) ? 2 : 1);
            }
        } while(true);
        return 2;
    }

    boolean isMentioned(Context context)
    {
        return false;
    }

    public Element toXML(Context context)
    {
        Element element = new Element("attribute", "http://www.brics.dk/DSD/2.0");
        if(name != null)
            Schema.addPname(element, name, name_uri);
        if(regexp != null)
            element.addContent(regexp.toXML(context));
        return element;
    }

    String name;
    String name_local;
    String name_uri;
    Regexp regexp;
}
